
<script>
import {timestampToCommonDate} from '@/utils/formatTime'
import {_array} from '@/utils/arrayExtension'
import {formData} from '@/wxRequest/requestApi'
export default {
	onLaunch(options) {},
	getOpenid() {},
	methods: {
		/**
		 * 检测是否授权 能否登录
		 */
		// 根据Code换取openid、sessionKey
		firstLogin() {
			let that = this
			wx.login({
				success: res => {
					let getParams = {
						url: '/login',
						data: {
							code: res.code,
						},
						method: 'POST',
					}
					this.$JZFormdata(getParams).then(re => {
						console.log('获取到了需要存入缓存的数据', re)

						// 存储根据Code换取的openid、sessionKey
						this.saveUserInfo(re)
						this.SecondGet()
					})
				},
			})
		},
		// 存储根据Code换取的openid、sessionKey
		saveUserInfo(re) {
			wx.setStorageSync('OpenIDandSessionKey', re.data)
			wx.setStorageSync('openid',re.data.openid )				
		},
		// 第二步根据openid、sessionKey换取微信用户私人信息
		SecondGet() {
			wx.getSetting({
				success: res => {
					if (res.authSetting['scope.userInfo']) {
						// 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
						wx.getUserInfo({
							success: response => {
								this.getSelfIfno(response)
							},
						})
					}
				},
			})
		},
		// 换取用户信息
		getSelfIfno(res) {
			// 可以将 res 发送给后台解码出 unionId
			wx.setStorageSync('userInfos', res)
			let mobile = wx.getStorageSync('userPhone')
			if (mobile != '') {
				let getParams = {
					url: '/login/userInfo ',
					data: {
						encryptedData: wx.getStorageSync('userInfos').encryptedData,
						sessionKey: wx.getStorageSync('OpenIDandSessionKey').sessionKey,
						iv: wx.getStorageSync('userInfos').iv,
						relationId: '1',
						mobile: mobile,
					},
					method: 'POST',
				}
				this.$JZFormdata(getParams).then(re => {
					this.$store.state.user.username = re.data.username
					this.$store.state.user.islogin = true
				})
			}
		},
	},

	created() {
		this.firstLogin()
		let that = this
		// wx.login({
		// 	success: res => {
		// 		if (res.code) {
		// 			let getParams = {
		// 				url: '/login',
		// 				data: {
		// 					code: res.code,
		// 				},
		// 				method: 'POST',
		// 			}
		// 			formData(getParams)
		// 				.then(re => {
		// 					//获取到的openid 存入缓存
		// 					wx.setStorageSync('loginUser', re.data)

		// 					wx.getSetting({
		// 						success: res => {
		// 							if (res.authSetting['scope.userInfo']) {
		// 								// 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
		// 								wx.getUserInfo({
		// 									success: res => {
		// 										// 可以将 res 发送给后台解码出 unionId
		// 										console.log('ccc', res)
		// 										wx.setStorageSync('userInfos', res)
		// 										let userInfoData = wx.getStorageSync('userInfos')
		// 										let mobile = wx.getStorageSync('userPhone')
		// 										console.log('用户需要绑定的手机', mobile)
		// 										let getParams = {
		// 											url: '/login/userInfo ',
		// 											data: {
		// 												encryptedData: userInfoData.encryptedData,
		// 												sessionKey: data.sessionKey,
		// 												iv: userInfoData.iv,
		// 												relationId: '1',
		// 												mobile: mobile,
		// 											},
		// 											method: 'POST',
		// 										}
		// 										formData(getParams).then(re => {
		// 											this.$store.state.user.username = re.data.username
		// 											this.$store.state.user.islogin = true
		// 										})
		// 										if (this.userInfoReadyCallback) {
		// 											this.userInfoReadyCallback(res)
		// 										}
		// 									},
		// 								})
		// 							}
		// 						},
		// 					})
		// 				})
		// 				.catch(re => {
		// 					wx.showToast({
		// 						title: '网络错误',
		// 						image: '../../static/images/gantanhao.png',
		// 					})
		// 				})
		// 		} else {
		// 			wx.showToast({
		// 				title: '网络错误',
		// 				image: '../../static/images/gantanhao.png',
		// 			})
		// 		}
		// 	},
		// })
	},
	mounted() {
		wx.onNetworkStatusChange(function(res) {
			wx.setStorageSync('networkType', res.networkType)
			if (!res.isConnected) {
				wx.showToast({
					title: '网络链接失败，请检查网络！',
					icon: 'none',
					duration: 5000,
				})
			}
		})
		wx.getNetworkType({
			success: function(res) {
				wx.setStorageSync('networkType', res.networkType)
			},
		})
	},
	onShow(options) {
		if (options.referrerInfo.appId) {
			wx.setStorageSync('referrerInfo', options.referrerInfo)
		}
	},
	onPageNotFound(res) {
		wx.redirectTo({
			url: 'pages/notFound/main',
		})
	},
	// onError(err) {
	// 	var pages = getCurrentPages()
	// 	if (pages.length <= 0) return
	// 	var currentPage = pages[pages.length - 1]
	// 	var url = currentPage.route
	// 	var options = JSON.stringify(currentPage.options)
	// 	var time = timestampToCommonDate(new Date().getTime())
	// 	var pageErrArr = wx.getStorageSync('pageErrArr') || []
	// 	pageErrArr = _array.unshift(pageErrArr, {url, options, err, time})
	// 	wx.setStorageSync('pageErrArr', pageErrArr)
	// },
}
</script>

<style lang="scss">
@import 'assets/iconfont.less';

page {
	box-sizing: border-box;
	background-color: $page-bgcolor;
	min-height: 100%;
	font-size: '14px';
}

.container {
	width: 100%;
	height: 100%;
	display: flex;
	flex-direction: column;
	box-sizing: border-box;
	background-color: $page-bgcolor;
}

/* this rule will be remove */
* {
	transition: width 2s;
	-moz-transition: width 2s;
	-webkit-transition: width 2s;
	-o-transition: width 2s;
}
</style>

